<?php
/*
Template Name: Stock Inventory
*/
?>

<?php
$aOptions = SofaSuppaStore::initOptions( false );
$num_of_mb = $aOptions[ 'num_of_def_boxes' ];
if( $num_of_mb <= 0 ) $num_of_mb = 1;

// colorbox
$cb_width = ( strlen( $aOptions[ 'cb_width' ] ) > 0 ) ? intval( $aOptions[ 'cb_width' ] ) : 660;
$cb_height = ( strlen( $aOptions[ 'cb_height' ] ) > 0 ) ? intval( $aOptions[ 'cb_height' ] ) : 450;
$cb_height_small = ( strlen( $aOptions[ 'cb_height_small' ] ) > 0 ) ? intval( $aOptions[ 'cb_height_small' ] ) : 300;
?>

<?php get_header(); ?>

<!-- main content start -->

	<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
    
    <div class="grid_12 clearfix">
    
    <!-- title start -->

    <h1><?php the_title(); ?></h1>
    
    <!-- title end -->
    
    <!-- pre-content (if any) start -->
    
    <?php the_content(); ?>
    
    <!-- pre-content (if any) end -->
    
    <!-- search label start -->
    
    <div class="grid_2 alpha">
        <span class="sfspan nomarg"><?php _e( "SEARCH TABLE", "sofa_suppastore" ); ?></span>
    </div>
    
    <!-- search label end -->
    
    <!-- search input start -->
    
    <div class="grid_5">
        <input type="text" class="grid_5 alpha" size="35" id="txtSearch" name="txtSearch" tabindex="998" />
    </div>
    
    <!-- search input end -->
    
    <!-- reset, expand buttons start --> 
    
    <div class="grid_2 omega prefix_3">
		<span id="imgSearch" class="addtocart srchfilterbig">
        	<a rel="nofollow" href="#"><?php _e( "Reset", "sofa_suppastore" ); ?></a>
        </span>
        <span style="display: block; float: left; width: 1px; height: 1px; line-height: 0;">&nbsp;</span>
    </div>
    
    <!-- reset, expand buttons end -->
    
    <!-- content start -->
    
    <div class="castshadow grid_12 clearfix alpha omega">&nbsp;</div>
    
    <?php
	// only Admin is allowed to see stock inventory table
	global $current_user;
	get_currentuserinfo();
    if( current_user_can( 'administrator' ) ) {
		
		$args = array(
			'post_type' => 'product',
			'post_status' => 'publish',
			'posts_per_page' => -1
		);
		// build WP query
		$my_query = new WP_Query( $args );
		if( $my_query->have_posts() ) : 
		$global_counter = 1;
	?>
    
    <!-- all products stock overview start -->
    
    <table id="tblmeta" width="100%" border="0" cellspacing="0" cellpadding="0">
    
        <tr id="tblheaders">
        	<th scope="col" class="textleft"><?php _e( "ID", "sofa_suppastore" ); ?></th>
            <th scope="col" class="center"><?php _e( "Image", "sofa_suppastore" ); ?></th>
            <th scope="col" class="center"><?php _e( "Name", "sofa_suppastore" ); ?></th>
            <th scope="col" class="center"><?php _e( "Size", "sofa_suppastore" ); ?></th>
            <th scope="col" class="textright"><?php _e( "In Stock", "sofa_suppastore" ); ?></th>
        </tr>
        
		<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
        
        <?php
		// class switch
        $class_switch = ' light';
        if( $global_counter % 2 == 0 ) $class_switch = ' dark';
		
		// product meta
		$arr_sizes = array();
		$arr_stock = array();
		
		$iter_meta = 0;
		while( $iter_meta < $num_of_mb ) {
			$single_price = 'sofa_' . $iter_meta . '_textarea_price';
			$single_sizes = 'sofa_' . $iter_meta . '_textarea_size';
			$single_stock = 'sofa_' . $iter_meta . '_textarea_stock';
			// populate array
			$single_id = get_the_ID(); // just to speed it up
			if( get_post_meta( $single_id, $single_price, true ) != '*' ) {
				array_push( $arr_sizes, get_post_meta( $single_id, $single_sizes, true ) );
				$stock_post_meta_value = get_post_meta( $single_id, $single_stock, true );
				if( $stock_post_meta_value == '*' || !$stock_post_meta_value ) array_push( $arr_stock, 0 );
				else array_push( $arr_stock, $stock_post_meta_value );
			}
			
			$iter_meta ++;
		}
        ?>
        
        <tr>
        
        <td class="textleft<?php echo $class_switch; ?>"><?php echo '<h4>#' . get_the_ID() . '</h4>'; ?></td>
        <td class="center<?php echo $class_switch; ?>">
		<?php 
		$img_attr = array( 'class'	=> 'size-thumbnail aligncenter topzero' );
		echo get_the_post_thumbnail( $post->ID, 'miniature', $img_attr );
		?>
        </td>
        <td class="center<?php echo $class_switch; ?>"><?php echo '<a href="' . get_permalink( get_the_ID() ) . '">' . get_the_title() . '</a>'; ?></td>
        <td class="center<?php echo $class_switch; ?>">
		<?php 
		$last_key_size = end( array_keys( $arr_sizes ) );
		foreach( $arr_sizes as $keys => $Vs ) { 
			if( $keys != $last_key_size ) echo '<span class="spanspaced">' . $Vs . '</span>' . '<span class="underline clearfix">&nbsp;</span>'; 
			else echo '<span class="spanspaced">' . $Vs . '</span>';
		} 
		?>
        </td>
        <td class="textright<?php echo $class_switch; ?>">
		<?php 
		$last_key_stock = end( array_keys( $arr_stock ) );
		foreach( $arr_stock as $keyst => $Vst ) {
			$edit_stock_q = '?itemid=' . get_the_ID() . '&amp;itemsize=' . urlencode( $arr_sizes[ $keyst ] ) . '&amp;itemstock=' . $Vst . '&amp;inrow=' . $keyst;
			if( $Vst <= 0 ) $out_of_stock_class = ' outofstock';
			else $out_of_stock_class = ' instock';
			echo '<a href="' . get_template_directory_uri() . '/edit_stock_item.php' . $edit_stock_q . '" class="stocker cboxElement">';
			if( $keyst != $last_key_stock ) echo '<span class="spanspaced' . $out_of_stock_class . '">' . $Vst . '</span>' . '<span class="underline clearfix">&nbsp;</span>'; 
			else echo '<span class="spanspaced' . $out_of_stock_class . '">' . $Vst . '</span>';
			echo '</a>';
		} 
		?>
        </td>
        
        </tr>
        
		<?php 
        $global_counter ++;
        endwhile; 
        ?>
    
    	<?php
		else :
    	echo '<tr id="infoline_1" class="proddetailstr"><td colspan="6">' . __( "No products found.", "sofa_suppastore" ) . '</td></tr>';
		endif;
		?>
    
    </table>
    
    <!-- all products stock overview end -->
    
    <?php
	
	}
	
	?>
    
    </div>

	<?php endwhile; ?>
    
	<script type='text/javascript'>
	
        // on-page table search functions
		
		jQuery.expr[ ":" ].containsNoCase = function( el, i, m ) {
            var search = m[ 3 ];
            if( !search ) return false;
			return new RegExp( search, "i" ).test( jQuery( el ).text() );
        };
    
        jQuery( document ).ready( function() {
            
            // colorbox
            jQuery( 'a.stocker' ).colorbox( { width: "<?php echo $cb_width . 'px'; ?>", height: "<?php echo $cb_height_small . 'px'; ?>", iframe: true } );
			
			// table search
            jQuery( '#imgSearch' ).hide();

            jQuery( '#imgSearch' ).click( function() {
                resetSearch();
				return false;
            });

            jQuery( '#txtSearch' ).keyup( function( event ) {
                if ( event.keyCode == 27 ) {
                    resetSearch();
                }
            } );

            // execute the search
            jQuery( '#txtSearch' ).keyup( function() {
                // only search when there are 3 or more characters in the textbox
                if( jQuery( '#txtSearch' ).val().length > 2 ) {
                    jQuery( '#tblmeta tr:first' ).show();
                    jQuery( '#tblmeta tr td:containsNoCase(\'' + jQuery( '#txtSearch' ).val() + '\')' ).parent().show();
                    jQuery( '#imgSearch' ).show();
					jQuery( '.norecords' ).remove();
                } else {
					jQuery( '#tblmeta tr:gt(0)' ).hide();
                    jQuery( '.norecords' ).remove();
                    jQuery( '#tblmeta' ).append( "<tr class='norecords'><td colspan='7'><?php _e( "Nothing found!", "sofa_suppastore" ); ?><\/td><\/tr>" );
				}
                if( jQuery( '#txtSearch' ).val().length == 0 ) {
                    resetSearch();
                }
				if( jQuery( '#tblmeta tr[id^=metabasket_]' ).css( 'display' ) == 'table-row' ) {
					jQuery( '.norecords' ).remove();
				}
            } );
			
		} );
		
        function resetSearch() {
            jQuery( '#txtSearch' ).val( '' );
            jQuery( '#tblmeta tr' ).show();
			jQuery( '#tblmeta tr[id^=metabasket_]' ).hide();
            jQuery( '.norecords' ).remove();
            jQuery( '#imgSearch' ).hide();
            jQuery( '#txtSearch' ).focus();
        }
		
	</script>
    
    <?php else : ?>
    
    <!-- no content start -->

	<div class="grid_12 clearfix">
    
        <h1><?php _e( "Nothing found!", "sofa_suppastore" ); ?></h1>
        <p><?php _e( "Sorry but no posts found. Take your time, there's gonna be soon to see something listed here.", "sofa_suppastore" ); ?></p>
    
    </div>
    
    <!-- no content end -->

	<?php endif; ?>
    
<!-- main content end -->

<?php get_footer(); ?>